package com.sunnada.btfvilg.controller;

import com.sunnada.btfvilg.common.response.Result;
import com.sunnada.btfvilg.common.response.ResultFactory;
import com.sunnada.btfvilg.entity.Address;
import com.sunnada.btfvilg.model.request.AddrParamVo;
import com.sunnada.btfvilg.model.request.UserParamVo;
import com.sunnada.btfvilg.model.request.UserRegParamVo;
import com.sunnada.btfvilg.model.response.AddrVo;
import com.sunnada.btfvilg.model.response.AddressListVo;
import com.sunnada.btfvilg.model.response.UserVo;
import com.sunnada.btfvilg.service.AddressService;
import com.sunnada.btfvilg.service.DeliverymanService;
import com.sunnada.btfvilg.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 地址管理接口
 */
@RestController
@RequestMapping("address")
@Api(tags = "地址管理接口")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/addAddress")
    @ApiOperation(value = "添加地址", httpMethod = "POST")
    public Result<AddrVo> addAddress(@Validated @RequestBody AddrParamVo addr) {
        AddrVo addrVo = addressService.addAddress(addr);
        return ResultFactory.success("添加成功",addrVo);
    }

    @GetMapping("/delAddress")
    @ApiOperation(value = "删除地址", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "地址id", dataType = "String", paramType = "query", required = true)
    })
    public Result<Object> delAddress(String aid) {
        addressService.delAddress(aid);
        return ResultFactory.success("删除成功", null);
    }

    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改地址", httpMethod = "POST")
    public Result<Object> updateAddress(@Validated @RequestBody AddrParamVo addr) {
        addressService.updateAddress(addr);
        return ResultFactory.success("修改成功",null);
    }

    @GetMapping("/getAddrByUid")
    @ApiOperation(value = "通过用户id获取地址", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "numPerPage", value = "每页记录数", dataType = "String", paramType = "query", required = true)
    })
    public Result<AddressListVo> getAddrByUid(String uid, Integer page, Integer numPerPage) {
        AddressListVo addrVo = addressService.getAddrByUid(uid, page, numPerPage);
        return ResultFactory.success("success", addrVo);
    }

    @GetMapping("/getAddrById")
    @ApiOperation(value = "通过地址id获取地址", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "用户id", dataType = "String", paramType = "query", required = true)
    })
    public Result<Address> getAddrById(String aid) {
        Address addr = addressService.getAddrById(aid);
        return ResultFactory.success("success", addr);
    }

}
